package net.dgg.rz.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.dgg.iboss.base.util.JsonUtil;
import net.dgg.rz.common.constant.ScProductConstant;
import net.dgg.rz.common.constant.SystemCommonConstant;
import net.dgg.rz.common.excption.CommonExcption;
import net.dgg.rz.common.utils.uniqueUtils.UniqueProductOrder;
import net.dgg.rz.production.dao.ProductOrderDao;
import net.dgg.rz.production.entity.ProductOrder;
import net.dgg.rz.production.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @className UniqueOrderUtils
 * @Author Quanlin Wan
 * @Description //TODO 订单唯一性验证工具
 * @Date 2019/3/19 15:04
 **/
@Component
public class UniqueOrderUtils {

    @Autowired
    ProductOrderDao productOrderDao;

    /**
     * @return
     * @Author Quanlin Wan
     * @Description //TODO 传入所有订单信息判断唯一性
     * @Date 19:37 2019/3/19
     * @Param allOrder json字符串
     **/
    public boolean uniqueOrder(String allOrder) {
        boolean res = false;
        List<JSONObject> allOrderList = JsonUtil.json2List(allOrder, JSONObject.class);
        Set<UniqueProductOrder> uniqueSet = new HashSet<>();
        for (JSONObject order : allOrderList) {
            Long productGoodsId = order.getLong("productId");
            Long channelId = order.getLong("channelId");
            ValidateUtils.strNotEmpty(productGoodsId, CommonExcption.class, "获取产品信息失败");
            ValidateUtils.strNotEmpty(channelId, CommonExcption.class, "获取渠道信息失败");
            UniqueProductOrder uniqueProductOrder = new UniqueProductOrder();
            uniqueProductOrder.setProductId(productGoodsId);
            uniqueProductOrder.setChannelId(channelId);
            uniqueSet.add(uniqueProductOrder);
        }
        if (uniqueSet.size() != allOrderList.size()) {
            res = true;
        }

        return res;
    }

    /**
     * @Author Quanlin Wan
     * @Description //TODO 适用于新增产品和更改产品的情况判断唯一性
     * @Date 20:02 2019/3/19
     * @Param proOrder 更改或新增的产品:更新{id:生产单id,productGoodsId:商品id,channelId:渠道id}，新增{productGoodsId:商品id,channelId:渠道id}
     * @Param orderId 销售订单id
     * @Param operaType 更改"change", 新增 "add"
     * @return res true 为存在重复项，false为不存在重复
     **/

    public boolean uniqueOrder(JSONObject modifyData,Long orderId,String operaType) {
        boolean res = false;
        List<ProductOrder> productOrders = new ArrayList<ProductOrder>();
        Set<UniqueProductOrder> uniqueSet = new HashSet<>();
        List<String> statusAttr = new ArrayList<>();
        statusAttr.add(ScProductConstant.RZSCORDER_STATUS21);
        switch (operaType) {
            case SystemCommonConstant.PRODUCT_ADD:
                productOrders=productOrderDao.selectByOrderIdSelective(orderId,statusAttr);
                break;
            case SystemCommonConstant.PRODUCT_CHANGE:
                productOrders=productOrderDao.selectByOrderIdSelective(orderId,statusAttr);
                break;
            default:
               break;
        }
        ValidateUtils.strNotEmpty(productOrders, CommonExcption.class, "未查询到订单信息");
        for (ProductOrder productOrder : productOrders) {
            UniqueProductOrder uniqueProductOrder = new UniqueProductOrder();
            uniqueProductOrder.setChannelId(productOrder.getChannelId());
            uniqueProductOrder.setProductId(productOrder.getProductId());
            uniqueSet.add(uniqueProductOrder);
        }
        uniqueSet.add(JSON.toJavaObject(modifyData, UniqueProductOrder.class));
        if (uniqueSet.size() != (productOrders.size() + 1)) {
            res = true;
        }
        return res;
    }
}


